//
//  HJMainTabBar.swift
//  Weibo
//
//  Created by 韩佳 on 16/4/16.
//  Copyright © 2016年 ddg. All rights reserved.
//

import UIKit

class HJMainTabBar: UITabBar {
    var composeButtonClosure:(()-> ())?
    // MARK: - 添加按钮
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(composeButton)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 布局按钮
    override func layoutSubviews() {
        super.layoutSubviews()
        composeButton.center = CGPointMake(self.bounds.width / 2, self.bounds.height / 2)
        
        var index: Int = 0
        let btnWidth: CGFloat = self.bounds.width / 5
        for value in subviews {
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
                value.frame.size.width = btnWidth
                value.frame.origin.x = CGFloat(index) * btnWidth
                index++
                if index == 2{
                    index++
                }
            }
        }
    }

    // MARK: - 懒加载按钮
    private lazy var composeButton: UIButton = {
        let button = UIButton()
        //添加点击事件
        button.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
//        addSubview(button)
        button.sizeToFit()
        return button
    }()
    // MARK: - 点击加号按钮调用
    @objc private func composeButtonClick(){
        
        composeButtonClosure?()
    }
}
